import {createRouter, createWebHistory} from 'vue-router'

import {useUserStore} from "@/stores/User.js";
import {ElMessage} from "element-plus";
let userStore = null;

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('@/views/home/Home.vue')
        }, {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login/Login.vue')
        }, {
            path: '/aiGen3d',
            name: 'aiGen3d',
            component: () => import('@/views/AIGen3D/AIGen3D.vue')
        }, {
            path: '/modelSquare',
            name: 'modelSquare',
            component: () => import('@/views/ModelSquare/ModelSquare.vue')
        }, {
            path: '/jobList',
            name: 'jobList',
            component: () => import('@/views/JobList/JobList.vue')
        }, {
            path: '/modelShowView',
            name: 'modelShowView',
            component: () => import('@/views/ModelShowView/ModelShowView.vue')
        }, {
            path:'/userInfo',
            name:'userInfo',
            component: () => import('@/views/UserInfo/UserInfo.vue')
        }, {
            path: '/userInfo/edit',
            name: 'userInfoEdit',
            component: () => import('@/views/UserInfo/UserInfoEdit.vue')
        }
    ],
});


router.beforeEach((to, from, next) => {
    if(userStore === null){
        userStore = useUserStore();
    }

    if (to.path === '/login') {
        next();
    }
    else if(to.path === '/'){
        next('/aiGen3d');
    }
    else {
        if (!userStore || !userStore.isLogin()) {
            ElMessage.info('请先登录')
            next('/login');
        } else {
            next();
        }
    }
});

export default router
